package com.magus.project.yddemo.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.magus.framework.core.controller.BaseController;
import com.magus.framework.core.persistence.SearchFilter;
import com.magus.framework.system.annotation.AppFunctionInfo;
import com.magus.framework.system.annotation.AppFunctionInfo.FunctionType;
import com.magus.framework.system.annotation.AppGroupInfo;
import com.magus.project.yddemo.entity.IndicatorDetail;
import com.magus.project.yddemo.service.IndicatorDetailService;
import com.magus.sis.common.entity.ComUnit;
import com.magus.sis.common.service.ComUnitService;

import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 
 * @author Yingjian Wang
 * @date 2018-01-25 11:30
 */
@ApiIgnore
@RestController
@AppGroupInfo(key = "yddemo:indicatorDetail", name = "指标下发管理", appKey = "yddemo")
@RequestMapping("/yddemo/indicatorDetails")
public class IndicatorDetailController extends BaseController {
	@Autowired
	private IndicatorDetailService indicatorDetailService;
	@Autowired
	private ComUnitService comUnitService;

	@GetMapping("/findAllComUnitByCompany")
	@RequiresPermissions("common:comUnit:view")
	@ApiOperation(value = "其它地方用到的机组类", notes = "其它地方用到的机组类")
	ResponseEntity<List<ComUnit>> findComUnits(@RequestParam(required = true) String companyId) {
		List<SearchFilter> searchFilters = new ArrayList<>();
		searchFilters.add(SearchFilter.equal("deleted", false));
		searchFilters.add(SearchFilter.equal("companyId", companyId));
		Specification<ComUnit> spec = buildFilterRequest(searchFilters);
		Sort sort = new Sort(Direction.ASC, "order");
		List<ComUnit> units = comUnitService.findAll(spec, sort);

		return ResponseEntity.ok(units);
	}

	@GetMapping
	@RequiresPermissions("yddemo:indicatorDetail:view")
	@AppFunctionInfo(key = "yddemo:indicatorDetail:view", name = "查询指标详细列表", groupKey = "yddemo:indicatorDetail", //
			isMenu = false, type = FunctionType.PAGE, url = "/yddemo/indicatorDetails")
	ResponseEntity<List<IndicatorDetail>> findAll(@RequestParam(required = true) String companyId,String dimension,
			@RequestParam(required = true) String packageId) {
		List<SearchFilter> searchFilters = new ArrayList<>();
		searchFilters.add(SearchFilter.equal("company.id", companyId));
		searchFilters.add(SearchFilter.equal("indicatorPackage.id", packageId));
		searchFilters.add(SearchFilter.equal("dimension", dimension));
		Specification<IndicatorDetail> spec = buildFilterRequest(searchFilters);
		List<IndicatorDetail> findAll = indicatorDetailService.findAll(spec);
		return ResponseEntity.ok(findAll);
	}

	/**
	 * 保存指标授权数据
	 * 
	 * @param indicatorRole
	 *            要保存的授权数据
	 * @param result
	 * @return
	 */
	@PutMapping
	@RequiresPermissions("yddemo:indicatorDetail:save")
	@ApiOperation(value = "保存指标详细数据", notes = "保存指标详细数据")
	@AppFunctionInfo(key = "yddemo:indicatorDetail:save", name = "保存指标详细数据", groupKey = "yddemo:indicatorDetail", //
			isMenu = false, type = FunctionType.BUTTON, url = "/yddemo/indicatorDetails")
	ResponseEntity<List<IndicatorDetail>> add(@RequestBody List<IndicatorDetail> indicatorDetail,
			BindingResult result) {
		if(indicatorDetail.get(0).getIndicator()==null) {
			List<SearchFilter> searchFilters = new ArrayList<>();
			searchFilters.add(SearchFilter.equal("indicatorPackage", indicatorDetail.get(0).getIndicatorPackage()));
			Specification<IndicatorDetail> spec = buildFilterRequest(searchFilters);
			List<IndicatorDetail> findAll = indicatorDetailService.findAll(spec);
			indicatorDetailService.delete(findAll);
			return ResponseEntity.ok(indicatorDetail);
		}
		validate(indicatorDetail, result);
		List<SearchFilter> searchFilters = new ArrayList<>();
		searchFilters.add(SearchFilter.equal("comUnit", indicatorDetail.get(0).getComUnit()));
		searchFilters.add(SearchFilter.equal("dimension", indicatorDetail.get(0).getDimension()));
		searchFilters.add(SearchFilter.equal("indicatorPackage", indicatorDetail.get(0).getIndicatorPackage()));
		Specification<IndicatorDetail> spec = buildFilterRequest(searchFilters);
		List<IndicatorDetail> findAll = indicatorDetailService.findAll(spec);
		indicatorDetailService.delete(findAll);
		List<IndicatorDetail> save = indicatorDetailService.save(indicatorDetail);
		return ResponseEntity.status(HttpStatus.CREATED).body(save);
	}
}
